package com.etc.MapReduce;


import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

public class OneTest {


    public static class OneMapper extends Mapper<LongWritable, Text,Text, Text> {
        String fileName = "";
        Text k = new Text();
        Text v = new Text();
        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            //获取文件名
            FileSplit split = (FileSplit) context.getInputSplit();
            fileName = split.getPath().getName();
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            //文本内容强制转换
            String line = value.toString();
            //按照空格切分
            String[] Array = line.split(" ");
            for(String s : Array){
                //将单词和文件名设置为键
                k.set(s + "-" + fileName);
                //value为空，为了计数
                context.write(k,v);
            }
        }
    }
    public static class Combiner extends Reducer<Text, Text,Text, Text> {
        Text k = new Text();
        Text v = new Text();
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            int line = 0;//次数
            for(Text value : values){
                line ++;
            }
            //将键以"-"重新拆分为单词
            String[] str = key.toString().split("-");
            k.set(str[0]);
            //输出格式为：单词-->次数
            v.set(str[1] + "-->" + line);
            context.write(k,v);
        }
    }
    public static class OneReduce  extends Reducer<Text, Text,Text,Text>{
        String line = "";
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            //遍历每个单词在哪个文件出现的次数
            for(Text value : values){
                line += value.toString();
            }
            context.write(key,new Text(line));
        }
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        //创建一个mapreduce的工作
        Job job =Job.getInstance(conf);

        job.setJarByClass(OneTest.class);
        //map的class在哪里
        job.setMapperClass(OneTest.OneMapper.class);
        //设置combiner的类
        job.setCombinerClass(OneTest.Combiner.class);
        //reduce的calss在哪里
        job.setReducerClass(OneTest.OneReduce.class);
        //map的输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        //reduce的输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        //判断文件是否存在，存在则删除
        File file = new File("f:\\output");
        if(file.exists()){
            FileUtils.deleteDirectory(file);
        }
        //计算的文件在哪里
        FileInputFormat.setInputPaths(job,new Path("f:\\intput"));
        FileOutputFormat.setOutputPath(job,new Path("f:\\output"));

        job.waitForCompletion(true);
    }
}
